import axios from 'axios'

const locationModule = {
  state: {
    latitude: '', // 纬度
    longitude: '', // 经度
    location: '', // 位置
  },
  actions: {
    locate (context) {
      axios({
        url: 'https://elm.cangdu.org/v1/cities?type=guess',
        method: 'GET',
      }).then(response => {
        // console.log(response.data)
        context.commit('handleLoaction', { latitude: response.data.latitude, longitude: response.data.longitude })
      })
      // console.log(context, value)
    },
    place (context, value) {
      axios({
        url: `https://elm.cangdu.org/v2/pois/${value.latitude},${value.longitude}`,
        method: 'GET',
      }).then(data => {
        context.commit('showPlace', data.data)
      })
    },
  },
  mutations: {
    handleLoaction (state, value) {
      state.latitude = value.latitude
      state.longitude = value.longitude
      /* 请求详细定位 */
      this.dispatch('place', { latitude: value.latitude, longitude: value.longitude })
      /* 获取相应店家信息  （已弃用） */
      // this.dispatch('nearByshop')
    },
    showPlace (state, value) {
      state.location = value.name
    },
  },
}
export default locationModule
